package com.inspur.cmis.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.FileUtils;
import org.apache.jasper.tagplugins.jstl.core.If;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.inspur.cmis.pojo.RegularMeet;
import com.inspur.cmis.pojo.RegularMeetExt;
import com.inspur.cmis.service.RegularMeetService;
import com.inspur.cmis.service.UserService;

@Controller
public class RegularMeetController {
	
	@Autowired
	private RegularMeetService regularMeetService;
	
	//分页显示全部例会信息
	@RequestMapping("/regularMeeting")
	public String getRegularMeetList(Model model,@RequestParam(value = "pn", defaultValue = "1") Integer pn,
			HttpServletRequest request) {
		//检索条件存放
		String mgrId = request.getParameter("mgrId");
		String meetDate = request.getParameter("meetDate");
		String theme = request.getParameter("theme");

		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("mgrId", mgrId);
		map.put("meetDate", meetDate);
		map.put("theme", theme);
		
		//分页
		PageHelper.startPage(pn, 5);
		List<RegularMeetExt> meetList = regularMeetService.getMeetList(map);
		PageInfo<RegularMeetExt> pageInfo = new PageInfo<RegularMeetExt>(meetList,meetList.size());

		//值的回显
		model.addAttribute("pageInfo", pageInfo);
		model.addAttribute("RmgrId", mgrId);
		model.addAttribute("RmeetDate", meetDate);
		model.addAttribute("Rtheme", theme);
		//迁移到周例会界面
		return "/regularMeeting/regularMeeting.jsp";
	}
	
	//添加例会信息
	@RequestMapping("/regularMeetAdd")
	public String regularMeetAdd(RegularMeet regularMeet, Model model,
			MultipartFile upload, HttpServletRequest req) {
		String fileName = "";
		try {// 原始⽂件名称
			if (upload != null) {
				fileName = upload.getOriginalFilename();//原始⽂件名称
				
				// 上传图片物理路径,将图片上传到哪里
				String url = req.getSession().getServletContext()
						.getRealPath("/upload/meet");
				File uploadfile = new java.io.File(url + "/" + fileName);
				if (!uploadfile.exists()) {
					uploadfile.mkdirs();
				}
				upload.transferTo(uploadfile);//将要上传的文件进行写到uploadfile指定的文件中
			}

		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//将选择的文件添加到数据库中
		regularMeet.setAttachment(fileName);
		
		regularMeetService.addRegularMeeting(regularMeet);
		model.addAttribute("saveFlag", "1");
		model.addAttribute("meet", regularMeet);
		model.addAttribute("message", "保存成功");
		return "/regularMeeting/regularMeetingAdd.jsp";
	}

	
	//点击文件超链接下载文件
	@RequestMapping("/downLoadFile")
	public ResponseEntity<byte[]> downFile(String fileName,
			HttpServletRequest req) throws IOException {
		String url = req.getSession().getServletContext()
				.getRealPath("/upload/meet");
		File file = new File(url + "/" + fileName);
		HttpHeaders headers = new HttpHeaders();
		// 下载显示的⽂件名，解决中⽂名称乱码问题
		String downloadFielName = new String(fileName.getBytes("UTF-8"),
				"iso-8859-1");
		// 通知浏览器以attachment（下载⽅式）打开图⽚
		headers.setContentDispositionFormData("attachment", downloadFielName);
		// application/octet-stream：⼆进制流数据（最常⻅的⽂件下载）。
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),
				headers, HttpStatus.CREATED);

	}

}
